<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
      :disabled="formType=='query'"
    >
      <el-form-item label="任务号" prop="taskNumber">
        <el-input v-model="formData.taskNumber" placeholder="请输入任务号" disabled />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="formData.taskName" placeholder="请输入任务名称" disabled />
      </el-form-item>
      <el-form-item label="任务备注" prop="taskRemark">
        <el-input v-model="formData.taskRemark" placeholder="请输入任务备注" type="textarea"  />
      </el-form-item>
      <el-form-item label="是否已开票" prop="isInvoiced">
        <el-checkbox v-model="formData.isInvoiced" :true-label="1" :false-label="0">已开票</el-checkbox>
      </el-form-item>
      <el-form-item label="收款日期" prop="paymentDate">
        <el-date-picker
          v-model="formData.paymentDate"
          type="date"
          value-format="x"
          placeholder="选择收款日期"
        />
      </el-form-item>
      <el-form-item label="收费金额" prop="paymentAmount">
        <el-input v-model="formData.paymentAmount" placeholder="请输入收费金额" />
      </el-form-item>
      <el-form-item label="收款方式" prop="paymentMethod">
        <el-select v-model="formData.paymentMethod" placeholder="请选择收款方式">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_PAY_METHOD)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收款公司" prop="collectionCompany">
        <el-input v-model="formData.collectionCompany" placeholder="请输入收款公司" />
      </el-form-item>
      <el-form-item label="付款公司" prop="paymentCompany">
        <el-input v-model="formData.paymentCompany" placeholder="请输入付款公司" />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ServiceFeesApi, ServiceFeesVO } from '@/api/oa/servicefees'

/** 事项办理费用管理 表单 */
defineOptions({ name: 'ServiceFeesForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  taskNumber: undefined,
  taskName: undefined,
  taskRemark: undefined,
  isInvoiced: 0,
  paymentDate: undefined,
  paymentAmount: undefined,
  paymentMethod: undefined,
  collectionCompany: undefined,
  paymentCompany: undefined,
  files: undefined,
  remark: undefined,
})
const formRules = reactive({
  // taskNumber: [{ required: true, message: '任务号不能为空', trigger: 'blur' }],
  // taskName: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
  // isInvoiced: [{ required: true, message: '请选择是否已开票', trigger: 'change' }],
  // paymentDate: [{ required: true, message: '收款日期不能为空', trigger: 'change' }],
  // paymentAmount: [{ required: true, message: '收费金额不能为空', trigger: 'blur' }],
  // paymentMethod: [{ required: true, message: '收款方式不能为空', trigger: 'change' }],
  // collectionCompany: [{ required: true, message: '收款公司不能为空', trigger: 'blur' }],
  // paymentCompany: [{ required: true, message: '付款公司不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ServiceFeesApi.getServiceFees(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ServiceFeesVO
    if (formType.value === 'create') {
      await ServiceFeesApi.createServiceFees(data)
      message.success(t('common.createSuccess'))
    } else {
      await ServiceFeesApi.updateServiceFees(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    taskNumber: undefined,
    taskName: undefined,
    taskRemark: undefined,
    isInvoiced: 0,
    paymentDate: undefined,
    paymentAmount: undefined,
    paymentMethod: undefined,
    collectionCompany: undefined,
    paymentCompany: undefined,
    files: undefined,
    remark: undefined,
  }
  formRef.value?.resetFields()
}
</script>